package com.unc.allergyex;


import java.util.ArrayList;

import com.unc.allergyex.helpers.ObjManager;
import com.unc.allergyex.objects.AllergyPreference;
import com.unc.allergyex.adapters.AllergyPreferenceAdapter;

import com.unc.allergyex.R;
import android.app.ListActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Activity responsible for showing and changing the allergies of the user.
 * 
 * @author Ryan Miller
 *
 */

public class Preferences extends ListActivity {
	
	private ArrayList<AllergyPreference> preferencesList;
	private SharedPreferences allergies;
	private SharedPreferences.Editor editor;
	private Context context;
	
	/**
	 * Standard function called on the launch of an Android Activity.
	 * Displays the screen according to the layout in layout/preferences.xml, 
	 * and sets an {@link AllergyPreferenceAdapeter} to display the layout in layout/preferencesitem.xml for each allergy.
	 */

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.preferences);
		allergies = getSharedPreferences(AllergyEX.ALLERGY_PREFS, 0);

		preferencesList = ObjManager.getAllergyPreferenceList();
		setListAdapter(new AllergyPreferenceAdapter(this,
				R.layout.preferencesitem, preferencesList));
		ListView lv = getListView();
		lv.setOnItemClickListener(mItemClickListener);

	}
	
	/**
	 * Listener called whenever an allergy list item is clicked.
	 * Toggles the display of the clicked allergen between checked with red background (allergic) and unchecked with white background (not allergic).
	 * Also toggles and saves whether the user is allergic to this allergen in the associated preferences file, whose name is defined by a constant in {@link AllergyEX}.
	 */
	
	public final OnItemClickListener mItemClickListener = new OnItemClickListener(){
		public void onItemClick(AdapterView<?> parent, View view,
		        int position, long id) {
			editor = allergies.edit();
			RelativeLayout background = (RelativeLayout) view;
			CheckBox checkbox = (CheckBox) background
					.findViewById(R.id.checkBox);

			AllergyPreference preference = preferencesList.get(position);
			if (preference.getAllergic()) {
				preference.setAllergic(false);
				TextView allergyName = (TextView) background.findViewById(R.id.allergyName);
				background.setBackgroundColor(0xFFFFFFFF);
				allergyName.setTextColor(0xFF000000);
				checkbox.setChecked(false);

			} else {
				preference.setAllergic(true);
				TextView allergyName = (TextView) background.findViewById(R.id.allergyName);
				background.setBackgroundColor(0xFFA50030);
				allergyName.setTextColor(0xFFFFFFFF);
				checkbox.setChecked(true);
			}
			editor.putBoolean(preference.getAllergen().getId(),
					preference.getAllergic());
			boolean success = editor.commit();
			if (!success) {
				Toast.makeText(context, "Failed to update preferences",
						Toast.LENGTH_SHORT).show();
			}
		}
	};
	
	/**
	 * Closes this activity in the event that the Back button is clicked
	 * 
	 * @param v the view that executed this callback when it was clicked
	 */
	
	public void returnButtonClicked(View v){
		this.finish();
	}
}

